package boot.controller.Shopping;

import boot.common.CommonResult;
import boot.domain.wxGoods.Goods;
import boot.domain.wxGoodsCart.GoodsShopcart;
import boot.model.userCarts;
import boot.model.userGoods;
import boot.model.wxResEntity;
import boot.service.orderService;
import boot.service.wxGoodService;
import boot.service.wxShoppingService;
import boot.service.wxUserService;
import boot.util.JwtUtil;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.Claims;
import io.micrometer.core.lang.Nullable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
public class userShoping {
    @Autowired
    private wxShoppingService wxShoppingService;
    @Autowired
    private wxUserService wxUserService;
    @Autowired
    private orderService orderService;
    @Autowired
    private wxGoodService wxGoodService;

    @PostMapping("/wxUser/addToShopCart")
    public CommonResult addToShopCart(@RequestHeader("Authorization") String token, @RequestParam("num") int num, @RequestParam("goodId") int goodId) {
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            int userId = wxUserService.getIdByOpenId(openid);
            wxShoppingService.add(goodId, num, userId);
            return new CommonResult(200, "添加成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult(400, "添加失败", null);
        }
    }

    @GetMapping("/wxUser/createOrder")
    public CommonResult createOrder(@RequestHeader("Authorization") String token, @RequestParam (value = "imm",required = false,defaultValue = "false") boolean imm,@RequestParam(value = "goodId",required = false)String goodId) {
        int userId;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            userId = wxUserService.getIdByOpenId(openid);
        } catch (Exception e) {
            return new CommonResult(400, "添加失败5", null);
        }
        if (!imm) {
            try {
                List<GoodsShopcart> list = wxShoppingService.CommitOrder(userId);
                if (list.size() != 0) {
                    StringBuilder cartsId = new StringBuilder();
                    System.out.println(list);
                    for (int i = 0; i < list.size(); i++) {
                        int id = list.get(i).getId();
                        cartsId.append(id);
                        if (i == list.size() - 1) break;
                        cartsId.append(",");
                    }
                    System.out.println(cartsId);
                    try {
                        if (orderService.createOrder(userId, cartsId) == 1) {
                            return new CommonResult(200, "添加成功", null);
                        }
                        return new CommonResult(400, "添加失败1", null);
                    } catch (Exception e) {
                        return new CommonResult(400, "添加失败2", null);
                    }
                }
                return new CommonResult(400, "添加失败3", null);
            } catch (Exception e) {
                e.printStackTrace();
                return new CommonResult(400, "添加失败4", null);
            }
        } else {
            try {
                StringBuilder cartsId = new StringBuilder();
                int cartId = wxShoppingService.add(Integer.parseInt(goodId), 1, userId);
                System.out.println(cartId);
                wxShoppingService.setCartStatus(cartId);
                System.out.println(cartId);
                cartsId.append(cartId);
                if (orderService.createOrder(userId, cartsId) == 1) {
                    return new CommonResult(200, "添加成功", null);
                }
                return new CommonResult(400, "添加失败1", null);
            } catch (Exception e) {
                return new CommonResult(400, "添加失败2", null);
            }
        }
    }
    @GetMapping("/wxUser/getUserCart")
    public CommonResult<userCarts> getUserCart(@RequestHeader("Authorization") String token) {
        int userId = 0;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            userId = wxUserService.getIdByOpenId(openid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //包含用户id和他的购物车
        userCarts userCarts = new userCarts();
        List<userGoods> goods1 = new ArrayList<userGoods>();
        //获取购物车
        List<GoodsShopcart> carts = wxShoppingService.getCarts(userId);
//        System.out.println(carts);
        for (GoodsShopcart item :
                carts) {
            int id = item.getGoodsId();
//            System.out.println(id);
            Goods goods = wxGoodService.getGoodById(id);
            userGoods usergoods = new userGoods(item.getNum(), userId, goods, item.getStatus());
            goods1.add(usergoods);
        }
        userCarts.setUserGoods(goods1);
        userCarts.setUserId(userId);
        return new CommonResult<>(200, "获取成功", userCarts);
    }

    //    @GetMapping("/wxUser/getCartByUserId")
//    public CommonResult getCartByUserId(){
//
//    }
    @GetMapping("/wxUser/changeGoodNum")
    public CommonResult changeGoodNum(@RequestHeader("Authorization") String token, @RequestParam("num") int num, @RequestParam("goodId") int goodId) {
        int userId = 0;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            userId = wxUserService.getIdByOpenId(openid);
            wxShoppingService.changeGoodNum(userId, goodId, num);
            return new CommonResult<>(200, "添加成功", null);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "添加失败", null);
        }


    }

    @GetMapping("/wxUser/deleteGood")
    public CommonResult deleteGood(@RequestHeader("Authorization") String token, @RequestParam("goodId") int goodId) {
        int userId = 0;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            userId = wxUserService.getIdByOpenId(openid);
            if (wxShoppingService.deleteGood(userId, goodId) == 1) {
                return new CommonResult<>(200, "删除成功", null);
            } else {
                return new CommonResult<>(400, "删除失败", null);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "删除失败", null);
        }
    }

    @GetMapping("/wxUser/goodChecked")
    public CommonResult goodChecked(@RequestHeader("Authorization") String token, @RequestParam("goodId") int goodId, @RequestParam("status") Byte status) {
        int userId = 0;
        try {
            Claims claims = JwtUtil.parseJWT(token);
            assert claims != null;
            wxResEntity wxResEntity = JSON.parseObject(claims.getSubject(), boot.model.wxResEntity.class);
            String openid = wxResEntity.getOpenid();
            userId = wxUserService.getIdByOpenId(openid);
            if (wxShoppingService.goodChecked(userId, goodId, status) == 1) {
                return new CommonResult<>(200, "", null);
            } else {
                return new CommonResult<>(400, "", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<>(400, "", null);
        }
    }
}
